package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;

@Configuration
public class OrderMQConfig {

    //延时队列
    @Bean
    public Queue orderDelayQueue(){
        //自定义参数
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl", 6000);//有效期（单位：毫秒），到期未被签收就变成死信
        arguments.put("x-dead-letter-exchange", "order-event-exchange");//消息变成死信后发送到指定的交换机
        arguments.put("x-dead-letter-routing-key", "order.release");//死信发送到交换机的路由键

        Queue queue = new Queue("order.delay.queue", true, false, false, arguments);
        return queue;
    }

    //队列
    @Bean
    public Queue orderReleaseQueue(){
        Queue queue = new Queue("order.release.queue", true, false, false);
        return queue;
    }
    //队列
    @Bean
    public Queue orderSeckillQueue(){
        Queue queue = new Queue("order.seckill.queue", true, false, false);
        return queue;
    }


    //交换机
    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange("order-event-exchange", true, false);
    }

    //绑定关系
    @Bean
    public Binding orderSeckillBinding(){
        return new Binding("order.seckill.queue", Binding.DestinationType.QUEUE,"order-event-exchange","order.seckill",null);
    }

    //绑定关系
    @Bean
    public Binding orderCreateBinding(){
        return new Binding("order.delay.queue", Binding.DestinationType.QUEUE,"order-event-exchange","order.create",null);
    }

    //绑定关系
    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding("order.release.queue", Binding.DestinationType.QUEUE,"order-event-exchange","order.release",null);
    }

    //绑定关系
    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding("stock.release.queue", Binding.DestinationType.QUEUE,"order-event-exchange","order.release.other.#",null);
    }
}
